<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>提交作业</title>
    <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body {
            margin: 0;
            padding: 0;
            height: 100vh;
            background-image: url('../../static/images/styles.png?v=1');

            background-size: cover;
            background-position: center;
            font-family: 'Arial', sans-serif;
        }

        .overlay {
            height: 100%;
            background: rgba(255, 255, 255, 0.7); /* 半透明白色背景 */
        }

        .form-container {
            max-width: 400px;
            margin: auto;
            padding: 20px 30px;
            border-radius: 10px;
            background-color: rgba(255, 255, 255, 0.9); /* 透明度高一点的白色 */
            box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
            position: relative;
            top: 50%;
            transform: translateY(-50%);
        }

        .form-container h2 {
            text-align: center;
            color: #28a745;
            font-weight: bold;
            margin-bottom: 20px;
        }

        .form-container label {
            font-weight: 600;
            color: #495057;
        }

        .form-container .btn {
            width: 100%;
            background-color: #28a745;
            border-color: #28a745;
        }

        .form-container .btn:hover {
            background-color: #218838;
            border-color: #1e7e34;
        }

        .back-link {
            position: absolute;
            top: 20px;
            left: 20px;
            font-size: 16px;
            color: #6c757d;
        }

        .back-link:hover {
            text-decoration: underline;
            color: #343a40;
        }
    </style>
</head>
<body>
    <div class="overlay">
        <a href="#" class="back-link">返回</a>
        <div class="form-container">
            <h2>提交作业</h2>
            <form id="submitAssignment" enctype="multipart/form-data">
                <div class="form-group">
                    <label for="file">上传文件</label>
                    <input type="file" id="file" name="file" class="form-control">
                </div>
                <button type="submit" class="btn btn-success">提交</button>
            </form>
        </div>
    </div>

    <!-- 使用 Fetch API 提交表单 -->
    <script>
        document.getElementById('submitAssignment').addEventListener('submit', function(event) {
            event.preventDefault();

            const formData = new FormData();
            formData.append('file', document.getElementById('file').files[0]);
            formData.append('assignment_id', {{ assignment_id }});

            fetch('/assignments/assignment/submit_assignment', {
                method: 'POST',
                body: formData
            })
            .then(response => response.json())
            .then(data => {
                if (data.error) {
                    alert(data.error);
                }
                if (data.message) {
                    alert('提交作业成功！');
                    window.location.href = '/classes/class/class_dashboard/{{ class_id }}';
                }
            })
            .catch(error => {
                alert('提交失败，请稍后再试。');
            });
        });
    </script>
</body>
</html>
